package com.wpt.sbtest.service;

import com.wpt.sbtest.domain.Girl;
import com.wpt.sbtest.enums.ResultEnum;
import com.wpt.sbtest.exception.GirlException;
import com.wpt.sbtest.repository.GirlRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;

@Service
public class GirlService {
    @Autowired
    private GirlRepository girlRepository;

    @Transactional
    public void saveTwo(){
        Girl girlA = new Girl();
        girlA.setAge(11);
        girlA.setCupSize("F");
        girlRepository.save(girlA);

        Girl girlB = new Girl();
        girlB.setAge(114);
        girlB.setCupSize("FG");
        girlRepository.save(girlB);
    }

    public void getAge(Integer id) throws GirlException {
        Girl girl = girlRepository.findById(id).orElse(null);
        Integer age = girl.getAge();
        if(age<10){
            throw new GirlException(ResultEnum.PRIMARY_SCHOOL);
        } else if(age > 10 && age < 16) {
            throw new GirlException(ResultEnum.MIDDLE_SCHOOL);
        }
    }

    /**
     * 通过id查询
     * @param id
     * @return
     */
    public Girl findOne(Integer id){
        return girlRepository.findById(id).orElse(null);
    }
}
